﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class RecoverPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string version = "";
            try { version = ConfigurationManager.AppSettings["version"]; }
            catch { version = "999.0.0.0"; }

            NoamStylesGlobalLiteral.Text = "<link href=\"NoamStyles/Global.css?v=" + version + "\" rel=\"stylesheet\" type=\"text/css\" />";
            NoamStylesDefaultLiteral.Text = "<link href=\"NoamStyles/Default.css?v=" + version + "\" rel=\"stylesheet\" type=\"text/css\" />";
            StylesheetNewLiteral.Text = "<link href=\"StyleSheetNew.css?v=" + version + "\" rel=\"stylesheet\" type=\"text/css\" />";

            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ForgotThePassword_PageTitle;
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/TeamHomePage.aspx");

            CultureInfo Cult = ResolveCulture();
            if ((Cult != null) && (Cult.TextInfo.IsRightToLeft))
            {
                CorrectDirPanel.Direction = ContentDirection.RightToLeft;
                DirStylesheetLiteral.Text = "<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheetRTL.css?v=" + version + "\" />";
                FacebookPanel.Direction = ContentDirection.RightToLeft;
                PostCodePanel.Direction = ContentDirection.RightToLeft;
                (LoginView1.FindControl("LoginPanel") as Panel).Direction = ContentDirection.RightToLeft;
            }
            else
            {
                DirStylesheetLiteral.Text = "<link type=\"text/css\" rel=\"stylesheet\" href=\"stylesheetLTR.css?v=" + version + "\" />";
            }
        }

        public void SendInstructions(object sender, EventArgs e)
        {
            string Name = UserName.Text.Trim();

            if (RegNoBot.IsValid() && (Name.Length > 0))
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                string Answer = GND.ReadNivdalStringData("UserName", "Language", "Nivdal_StartResetPasswordProcess", Name, ou.Language);

                GND.SendPendingMails();

                Step1Panel.Visible = false;
                CompletePanel.Visible = true;
                MessageLabel.Text = Answer;
            }
        }

        public static CultureInfo ResolveCulture()
        {
            string[] languages = HttpContext.Current.Request.UserLanguages;
            if (languages == null || languages.Length == 0)
                return null;
            try
            {
                string language = languages[0].ToLowerInvariant().Trim();
                return System.Globalization.CultureInfo.CreateSpecificCulture(language);
            }
            catch (ArgumentException)
            {
                return null;
            }
        }
    }
}